package org.mybatis.generator.plugins.fx;

import org.apache.log4j.Logger;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.GeneratedJavaScriptFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.plugins.fx.elements.FxMethodGenerator;
import org.mybatis.generator.plugins.vue.elements.VueMethodGenerator;

import java.util.ArrayList;
import java.util.List;

/**
 * cjianquan 2019/8/4
 */
public class MybatisFxPlugin extends PluginAdapter {

    private static final Logger logger = Logger.getLogger(MybatisFxPlugin.class);

    private static int SORT_ORDER = 10036;

    private String prePack;
    private String moduleName;
    private String outPath;
    private String prePath;

    @Override
    public boolean validate(List<String> warnings) {
        prePack = properties.getProperty("prePack");
        moduleName = properties.getProperty("moduleName");
        outPath = properties.getProperty("outPath");
        prePath = properties.getProperty("prePath");
        return true;
    }

    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(
            IntrospectedTable introspectedTable) {

        // 增加结束处
        return null;
    }

    @Override
    public boolean clientGenerated(Interface interfaze,
                                   TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        // 接口方法
        return true;
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document,
                                           IntrospectedTable introspectedTable) {
        return true;
    }

    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
            XmlElement element, IntrospectedTable introspectedTable) {
        return true;
    }

    @Override
    public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(
            XmlElement element, IntrospectedTable introspectedTable) {
        return true;
    }

    @Override
    public List<GeneratedJavaScriptFile> contextGenerateAdditionalJavaScriptFiles() {
        // TODO Auto-generated method stub
        List<GeneratedJavaScriptFile> files = new ArrayList<GeneratedJavaScriptFile>(
                1);
        return files;
    }

    @Override
    public List<GeneratedJavaScriptFile> contextGenerateAdditionalJavaScriptFiles(
            IntrospectedTable introspectedTable) {
        String tableName = introspectedTable.getTableConfiguration()
                .getTableName();
        String tableLab = introspectedTable.getRemarks();
        while (tableName.lastIndexOf("_") != -1) {
            int index = tableName.lastIndexOf("_");
            tableName = tableName.replaceFirst(
                    "_" + tableName.substring(index + 1, index + 2), tableName
                            .substring(index + 1, index + 2).toUpperCase());
        }
        tableName=toLowerCase(tableName);
        List<GeneratedJavaScriptFile> files = new ArrayList<GeneratedJavaScriptFile>();


        FxMethodGenerator emg = new FxMethodGenerator(introspectedTable,
                prePack,moduleName,prePath);

        GeneratedJavaScriptFile fxmlFile = new GeneratedJavaScriptFile(
                emg.getFxml(), properties.getProperty("fileName",
                toUpperCase(tableName) + ".fxml"), "resources/"+prePack+"/fxmlView/"+moduleName, outPath, true);

        /*GeneratedJavaScriptFile fxmlViewFile = new GeneratedJavaScriptFile(
                emg.getFxmlView(), properties.getProperty("fileName",
                toUpperCase(tableName) + "FxmlView.java"), "java/"+prePack+"/fxmlView/"+moduleName, outPath, true);*/

        GeneratedJavaScriptFile viewFile = new GeneratedJavaScriptFile(
                emg.getView(), properties.getProperty("fileName",
                toUpperCase(tableName) + "View.java"), "java/"+prePack+"/view/view/"+moduleName, outPath, true);

        GeneratedJavaScriptFile controllerFile = new GeneratedJavaScriptFile(
                emg.getController(), properties.getProperty("fileName",
                toUpperCase(tableName) + "Controller.java"), "java/"+prePack+"/view/controller/"+moduleName, outPath, true);

        GeneratedJavaScriptFile serviceFile = new GeneratedJavaScriptFile(
                emg.getService(), properties.getProperty("fileName",
                toUpperCase(tableName) + "Service.java"), "java/"+prePack+"/view/service/"+moduleName, outPath, true);


        files.add(fxmlFile);
//        files.add(fxmlViewFile);
        files.add(viewFile);
        files.add(controllerFile);
        files.add(serviceFile);

        SORT_ORDER++;
        return files;
    }


    /**
     * BaseUsers to baseUsers
     *
     * @param tableName
     * @return
     */
    protected String toLowerCase(String tableName) {
        StringBuilder sb = new StringBuilder(tableName);
        sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
        return sb.toString();
    }

    /**
     * BaseUsers to baseUsers
     *
     * @param tableName
     * @return
     */
    protected String toUpperCase(String tableName) {
        StringBuilder sb = new StringBuilder(tableName);
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        return sb.toString();
    }

}
